package com.lanxin.judge.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lanxin.judge.entity.LcFlow;
import com.lanxin.judge.service.ILcFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-08-01
 */
@Controller
@RequestMapping("judge/flow")
public class LcFlowController {
    @Autowired
    private ILcFlowService lcFlowService;

    //根据条件分页列表审批流程
    @RequestMapping("/listFlow")
    public ModelAndView listJudgeFlow(@RequestParam(required = false) Integer page,@RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String flowName,@RequestParam(required = false) String docType){
        IPage<LcFlow> flowIPage = lcFlowService.pageJudgeFlow(page,pageSize,flowName,docType);
        LcFlow.DocType[] docTypes = LcFlow.DocType.values();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("flowIPage",flowIPage);
        modelAndView.addObject("docTypes",docTypes);
        modelAndView.addObject("flowName",flowName);
        modelAndView.addObject("docType",docType);
        modelAndView.setViewName("judge/listFlow");
        return modelAndView;
    }

    @RequestMapping("/editFlow")
    public String editFlow(LcFlow flow){
        //oid存在则为修改请求
        if(flow.getOid()!=null){
            lcFlowService.updateFlow(flow);
            return "redirect:listFlow";
        }
        //oid不存在则为添加请求
        lcFlowService.addFlow(flow);
        return "redirect:listFlow";
    }

    //检测流程名称是否合法
    @RequestMapping("/checkFlowName")
    @ResponseBody
    public String checkFlowName(@RequestParam Integer oid, @RequestParam String flowName){
        boolean s = lcFlowService.checkFlowName(oid,flowName);
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        Map<String,Boolean> map = new HashMap<>();
        map.put("valid",s);
        try {
            json = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return json;
    }

    //更新流程状态
    @RequestMapping("/editFlowState")
    @ResponseBody
    public boolean editFlowState(Integer oid,String state){//oid为流程id
        //如果要启动流程，则需要检测改流程是否有审批节点，没有节点的流程返回false
        return lcFlowService.editFlowState(oid,state);
    }

    //删除流程
    @RequestMapping("/deleteFlow")
    @ResponseBody
    public boolean deleteFlow(Integer oid){
        return lcFlowService.deleteFlow(oid);
    }
}
